package com.vdg.lockphotos.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.vdg.lockphotos.DisplayImageActivity;
import com.vdg.lockphotos.R;
import com.vdg.lockphotos.app.ApplicationManagement;
import com.vdg.lockphotos.data.Photo;
import com.vdg.lockphotos.notify.OnLoadFileListner;
import com.vdg.lockphotos.notify.OnLoadListHiddenImageListener;
import com.vdg.lockphotos.notify.OnSelectMenuItemListener;
import com.vdg.lockphotos.util.ImageLoader;
import com.vdg.lockphotos.util.Utilities;
import com.vdg.lockphotos.view.TouchImageView;

public class ProductDetailFragment extends Fragment {

	TouchImageView pdtImg;
	Activity activity;

	ImageLoader imageLoader;
	DisplayImageOptions options;

	Photo product;

	public static final String ARG_ITEM_ID = "pdt_detail_fragment";
	private OnSelectMenuItemListener mListener = new OnSelectMenuItemListener() {

		@Override
		public void onSelectViewImage() {
		}

		@Override
		public void onSelectUnHide() {
			// TODO Auto-generated method stub
			if (Utilities.isExternalStorageReadable() == true
					&& Utilities.isExternalStorageWritable() == true) {
				ArrayList<Photo> listPhoto = new ArrayList<Photo>();
				listPhoto.add(product);
				ApplicationManagement.getInstance(getActivity()).unHidePhotos(
						listPhoto);
				if (activity != null) {
					activity.onBackPressed();
				}
			} else {
				Utilities.showToast(
						getResources().getString(R.string.sdcard_notready),
						getActivity());
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activity = getActivity();
		options = new DisplayImageOptions.Builder()
				.showImageOnFail(R.drawable.ic_launcher)
				.showStubImage(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher).cacheInMemory()
				.cacheOnDisc().build();
		imageLoader = ImageLoader.getInstance(getActivity());
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		((DisplayImageActivity) getActivity())
				.regisOnSelectMenuListener(mListener);
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_pdt_detail, container,
				false);
		findViewById(view);

		Bundle bundle = this.getArguments();
		if (bundle != null) {
			product = bundle.getParcelable("singleProduct");
			setProductItem(product);
		}

		return view;
	}

	private void findViewById(View view) {

		pdtImg = (TouchImageView) view.findViewById(R.id.imv_picture);
	}

	private void setProductItem(Photo resultProduct) {

		imageLoader.DisplayImage(resultProduct.getHiddenUrl(), pdtImg,
				new OnLoadFileListner() {

					@Override
					public void onSuccess(boolean isAnimation) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onStart() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub

					}
				}, 0, false);
	}
}
